package org.kailu.book.controller;

import org.kailu.book.entity.CustomerEntity;
import org.kailu.book.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * @author viet.ch
 * @generated May 25, 2014
 * @from Ho Chi Minh, VN
 */

@Controller
public class CustomerController {

	@Autowired
	private CustomerService customerService;
	
	@RequestMapping("/customer")
	public String getData(ModelMap map) {
		map.addAttribute("customerList",customerService.getAll());
		return "customer";
	}
	
	@RequestMapping(value = "/customer/saveorupdate", method = RequestMethod.GET)
	public String initData(ModelMap map) {
		map.addAttribute("customer",new CustomerEntity());
		return "customer_new";
	}
	
	@RequestMapping(value = "/customer/saveorupdate", method = RequestMethod.POST)
    public String addBooking(@ModelAttribute(value="customer") CustomerEntity customerEntity, BindingResult result, ModelMap map)
    {
		customerService.saveOrUpdate(customerEntity);
		return "redirect:/customer";
    }
	
	@RequestMapping(value = "/customer/saveorupdate/{customerId}", method = RequestMethod.GET)
    public String update(@PathVariable("customerId") int customerId, ModelMap map)
    {
		map.addAttribute("customer", customerService.get(customerId));
		return "customer_new";
    }
	
	@RequestMapping(value = "/customer/delete/{customerId}", method = RequestMethod.GET)
    public String delete(@PathVariable("customerId") int customerId)
    {
		this.customerService.delete(customerId);
		return "redirect:/customer";
    }
}
